﻿Public Class ApplicationMain
    Dim MyCsvConnector As CsvConnector
    Dim MyPdfFormTemplate As PdfTemplateInfo

    Private Sub btnOpenCsv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCsv.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not String.IsNullOrEmpty(OpenFileDialog1.FileName.Trim) Then
                txtSource.Text = OpenFileDialog1.FileName.Trim
                MyCsvConnector = New CsvConnector(OpenFileDialog1.FileName.Trim, ";")
                GvPreviewData.DataSource = Nothing
                GvPreviewData.DataSource = MyCsvConnector.SourceDt
            End If
        End If
    End Sub

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        DoIt()
        MsgBox("Pdf generation is now complete", MsgBoxStyle.Information, "Done")
    End Sub

    Private Sub DoIt()
        Dim CsvD As String

        If Not String.IsNullOrEmpty(txtDelimiter.Text.Trim) Then
            CsvD = txtDelimiter.Text.Trim
        Else
            CsvD = ";"
        End If

        Dim BaseName As String

        If Not String.IsNullOrEmpty(txtBaseFileName.Text.Trim) Then
            BaseName = txtBaseFileName.Text.Trim
        Else
            BaseName = "form_export"
        End If


        GeneratePdfs(BaseName, txtFormPath.Text, txtDestination.Text, MyCsvConnector, GvPreviewData.DataSource)
    End Sub

    Private Sub btnSelectForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectForm.Click
        If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not String.IsNullOrEmpty(OpenFileDialog2.FileName.Trim) Then
                txtFormPath.Text = OpenFileDialog2.FileName.Trim
            End If
        End If
    End Sub

    Private Sub btnBrowseDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseDestination.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not String.IsNullOrEmpty(FolderBrowserDialog1.SelectedPath) Then
                txtDestination.Text = FolderBrowserDialog1.SelectedPath
            End If
        End If
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        MsgBox("Created by Konstantin Kostov http://www.konstantinkostov.com" & Environment.NewLine & "Distributed under the GPL v3", MsgBoxStyle.Information, "About")
    End Sub
End Class
